<div class="layui-fluid layui-anim febs-anim" id="febs-sysLog" lay-title="系统日志"
     xmlns:shiro="http://www.w3.org/1999/xhtml">
  <div class="layui-row febs-container">
    <div class="layui-col-md12">
      <div class="layui-card">
        <div class="layui-card-body febs-table-full">
          <form class="layui-form layui-table-form" lay-filter="sysLog-table-form">
            <div class="layui-row">
              <div class="layui-col-md10">
                <div class="layui-form-item">
                  <div class="layui-inline">
                    <label class="layui-form-label layui-form-label-sm">用户名称</label>
                    <div class="layui-input-inline">
                      <input type="text" name="username" autocomplete="off" class="layui-input">
                    </div>
                  </div>
                  <div class="layui-inline">
                    <label class="layui-form-label layui-form-label-sm">方法描述</label>
                    <div class="layui-input-inline">
                      <input type="text" name="operation" autocomplete="off"
                             class="layui-input">
                    </div>
                  </div>
                  <div class="layui-inline">
                    <label class="layui-form-label layui-form-label-sm">ip地址</label>
                    <div class="layui-input-inline">
                      <input type="text" name="ip" autocomplete="off" class="layui-input">
                    </div>
                  </div>
                </div>
              </div>
              <div class="layui-col-md2 layui-col-sm12 layui-col-xs12 table-action-area">
                <div class="layui-btn layui-btn-sm layui-btn-primary table-action" id="query">
                  <i class="layui-icon">&#xe848;</i>
                </div>
                <div class="layui-btn layui-btn-sm layui-btn-primary table-action" id="reset">
                  <i class="layui-icon">&#xe79b;</i>
                </div>
                <div class="layui-btn layui-btn-sm layui-btn-primary table-action action-more"
                     shiro:hasAnyPermissions="sysLog:delete">
                  <i class="layui-icon">&#xe875;</i>
                </div>
              </div>
            </div>
          </form>
          <table lay-filter="sysLogTable" lay-data="{id: 'sysLogTable'}"></table>
        </div>
      </div>
    </div>
  </div>
</div>
<script type="text/html" id="sysLog-option-one" xmlns:shiro="http://www.w3.org/1999/xhtml">
  <span shiro:lacksPermission="sysLog:delete">
        <span class="layui-badge-dot febs-bg-orange"></span>无权限
    </span>
  <a lay-event="del" shiro:hasPermission="sysLog:delete">
    <i class="layui-icon febs-edit-area febs-red">&#xe7f9;</i></a>
</script>
<script data-th-inline="none" type="text/javascript">
  layui.use(['dropdown', 'jquery', 'form', 'table', 'febs'], function () {
    var $ = layui.jquery,
        febs = layui.febs,
        form = layui.form,
        table = layui.table,
        dropdown = layui.dropdown,
        $view = $('#febs-sysLog'),
        $query = $view.find('#query'),
        $reset = $view.find('#reset'),
        $searchForm = $view.find('form'),
        sortObject = {field: 'createtime', type: 'asc'},
        tableIns;


    form.render();

    initTable();

    dropdown.render({
      elem: $view.find('.action-more'),
      click: function (name, elem, event) {
        var checkStatus = table.checkStatus('sysLogTable');
        if (name === 'delete') {
          if (!checkStatus.data.length) {
            febs.alert.warn('请选择需要删除的日志信息');
          } else {
            febs.modal.confirm('删除信息', '确定删除该条日志信息？', function () {
              var sysLogIds = [];
              layui.each(checkStatus.data, function (key, item) {
                sysLogIds.push(item.id)
              });
              deletesysLogs(sysLogIds.join(','));
            });
          }
        }
      },
      options: [{
        name: 'delete',
        title: '删除信息',
        perms: 'sysLog:delete'
      }]
    });
    table.on('tool(sysLogTable)', function (obj) {
      var data = obj.data,
          layEvent = obj.event;
      if (layEvent === 'del') {
        febs.modal.confirm('删除信息', '是否删除该日志信息？', function () {
          deletesysLogs(data.id);
        });
      }
    });

    table.on('sort(sysLogTable)', function (obj) {
      sortObject = obj;
      tableIns.reload({
        initSort: obj,
        where: $.extend(getQueryParams(), {
          field: obj.field,
          order: obj.type
        })
      });
    });

    $query.on('click', function () {
      var params = $.extend(getQueryParams(), {field: sortObject.field, order: sortObject.type});
      tableIns.reload({where: params, page: {curr: 1}});
    });

    $reset.on('click', function () {
      $searchForm[0].reset();
      tableIns.reload({where: getQueryParams(), page: {curr: 1}, initSort: sortObject});
    });

    function initTable() {
      tableIns = febs.table.init({
        elem: $view.find('table'),
        id: 'sysLogTable',
        url: ctx + 'sysLog/list',
        cols: [[
          {type: 'checkbox'},
          {title: '用户名称', field: 'username', minWidth: 100},
          {title: '方法描述', field: 'operation'},
          {title: '方法耗时（s）', field: 'time'},
          {title: '详细访问', field: 'method'},
          {title: '参数', field: 'params'},
          {title: 'IP地址', field: 'ip', sort: true},
          {title: '创建时间', field: 'createtime', sort: true},
          {title: '操作地点', field: 'location', sort: true},
          {title: '操作', toolbar: '#sysLog-option-one', minWidth: 140}
        ]]
      });
    }

    function getQueryParams() {
      return {
        ip: $searchForm.find('input[name="ip"]').val().trim(),
        username: $searchForm.find('input[name="username"]').val(),
        operation: $searchForm.find('input[name="operation"]').val()
      };
    }

    function deletesysLogs(sysLogIds) {
      febs.post(ctx + 'sysLog/delete/' + sysLogIds, null, function () {
        febs.alert.success('删除成功');
        $query.click();
      });
    }
  })
</script>